package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Light;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 管理灯 Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/29 9:14
 * @Version: v1.0.0
 */
public class LightActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 1;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_name)
    AppCompatEditText mName;

    @BindView(R.id.et_type)
    AppCompatEditText mType;

    @BindView(R.id.et_sort)
    AppCompatEditText mSort;

    private static Integer tag;
    private static Long showcaseId;
    private static Light light;

    private static CommonDaoUtils<Light> lightDaoUtils = DaoUtilsStore.getInstance().getLightDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_light);

        ButterKnife.bind(this);
        tag = getIntent().getIntExtra("TAG", 1);
        showcaseId = getIntent().getLongExtra("showcaseId", -1);
        if (tag == 2) {
            light = FastJsonUtils.strToBean(getIntent().getStringExtra("light"), Light.class);
        }

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
        if (tag == 1) {
            mTitle.setTitle("添加控制灯");
        } else if (tag == 2) {
            mTitle.setTitle(light.getName());

            if (!TextUtils.isEmpty(light.getName())) {
                mName.setText(light.getName());
            }

            if (null != light.getType()) {
                mType.setText(String.valueOf(light.getType()));
            }

            if (null != light.getSort()) {
                mSort.setText(String.valueOf(light.getSort()));
            }
        }
    }

    private void initData() {

    }

    private void lightOperation() {
        String name = mName.getText().toString();
        String type = mType.getText().toString();
        String sort = mSort.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.warning("灯名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(type)) {
            ToastUtils.warning("灯控制路不能为空");
            return;
        }

        if (TextUtils.isEmpty(sort)) {
            ToastUtils.warning("排序不能为空");
            return;
        }

        if (1 == tag) {
            light = new Light();
            light.setShowcaseId(showcaseId);
            light.setName(name);
            light.setType(Integer.valueOf(type));
            light.setSort(Integer.valueOf(sort));
            light.setStatus(1);
            light.setProgress(0);

            long id = lightDaoUtils.insert(light);
            if (id > 0) {
                ToastUtils.success("操作成功");
                finish();
            } else {
                ToastUtils.error("操作失败");
            }
        } else {
            light.setName(name);
            light.setType(Integer.valueOf(type));
            light.setSort(Integer.valueOf(sort));

            boolean flag = lightDaoUtils.update(light);
            if (flag) {
                ToastUtils.success("操作成功");
                finish();
            } else {
                ToastUtils.error("操作失败");
            }
        }
    }

    @OnClick({R.id.btn_light})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_light:
                KeyboardUtils.hideSoftInput(LightActivity.this);
                SimpleDialogFragment.createBuilder(LightActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要操作吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .setCancelable(false)
                        .show();
                break;
            default:

                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            lightOperation();
        }
    }
}